package redis

import (
	"github.com/go-redis/redis/v8"
	"context"
)

// 向有序集合添加（更新）一个或多个成员
func (h *Handler) ZAdd(key string, scoreMembers ...*redis.Z) (count int64, err error) {
	count, err = h.Client.ZAdd(context.Background(), key, scoreMembers...).Result()
	go h.Log("ZSet-ZAdd", []interface{}{key, scoreMembers}, count, err)
	return
}

// 获取有序集合的成员的数量
func (h *Handler) ZCard(key string) (count int64, err error) {
	count, err = h.Client.ZCard(context.Background(), key).Result()
	go h.Log("ZSet-ZCard", key, count, err)
	return
}

// 计算在有序集合中指定区间分数的成员数
func (h *Handler) ZCount(key string, start string, end string) (count int64, err error) {
	count, err = h.Client.ZCount(context.Background(), key, start, end).Result()
	go h.Log("ZSet-ZCount", []interface{}{key, start, end}, count, err)
	return
}

// 有序集合中对指定成员的分数加上增量 increment
func (h *Handler) ZIncrBy(key string, incr float64, member string) (score float64, err error) {
	score, err = h.Client.ZIncrBy(context.Background(), key, incr, member).Result()
	go h.Log("ZSet-ZIncrBy", []interface{}{key, incr, member}, score, err)
	return
}

// 计算给定的一个或多个有序集的交集，并将结果集存储在新的有序集合 key 中
func (h *Handler) ZInterStore(destination string, opt *redis.ZStore) (count int64, err error) {
	count, err = h.Client.ZInterStore(context.Background(), destination, opt).Result()
	go h.Log("ZSet-ZInterStore", []interface{}{destination, opt}, count, err)
	return
}

// 在有序集合中计算指定字典区间内成员数量
func (h *Handler) ZLexCount(key, min, max string) (count int64, err error) {
	count, err = h.Client.ZLexCount(context.Background(), key, min, max).Result()
	go h.Log("ZSet-ZLexCount", []interface{}{key, min, max}, count, err)
	return
}

// 通过索引区间返回有序集合指定区间内的成员
func (h *Handler) ZRange(key string, start, end int64) (members []string, err error) {
	members, err = h.Client.ZRange(context.Background(), key, start, end).Result()
	go h.Log("ZSet-ZRange", []interface{}{key, start, end}, members, err)
	return
}

// 通过字典区间返回有序集合的成员
func (h *Handler) ZRangeByLex(key string, opt *redis.ZRangeBy) (members []string, err error) {
	members, err = h.Client.ZRangeByLex(context.Background(), key, opt).Result()
	go h.Log("ZSet-ZRangeByLex", []interface{}{key, opt}, members, err)
	return
}

// 通过分数返回有序集合指定区间内的成员
func (h *Handler) ZRangeByScore(key string, opt *redis.ZRangeBy) (members []string, err error) {
	members, err = h.Client.ZRangeByScore(context.Background(), key, opt).Result()
	go h.Log("ZSet-ZRangeByScore", []interface{}{key, opt}, members, err)
	return
}

// 返回有序集合中指定成员的索引
func (h *Handler) ZRank(key string, member string) (index int64, err error) {
	index, err = h.Client.ZRank(context.Background(), key, member).Result()
	go h.Log("ZSet-ZRank", []interface{}{key, member}, index, err)
	return
}

// 移除有序集合中的一个或多个成员
func (h *Handler) ZRem(key string, members ...interface{}) (count int64, err error) {
	count, err = h.Client.ZRem(context.Background(), key, members...).Result()
	go h.Log("ZSet-ZRem", []interface{}{key, members}, count, err)
	return
}

// 移除有序集合中给定的字典区间的所有成员
func (h *Handler) ZRemRangeByLex(key, min string, max string) (count int64, err error) {
	count, err = h.Client.ZRemRangeByLex(context.Background(), key, min, max).Result()
	go h.Log("ZSet-ZRemRangeByLex", []interface{}{key, min, max}, count, err)
	return
}

// 移除有序集合中给定索引区间的所有成员
func (h *Handler) ZRemRangeByRank(key string, start, end int64) (count int64, err error) {
	count, err = h.Client.ZRemRangeByRank(context.Background(), key, start, end).Result()
	go h.Log("ZSet-ZRemRangeByRank", []interface{}{key, start, end}, count, err)
	return
}

// 移除有序集合中给定的分数区间的所有成员，包含边界值
func (h *Handler) ZRemRangeByScore(key string, start string, end string) (count int64, err error) {
	count, err = h.Client.ZRemRangeByScore(context.Background(), key, start, end).Result()
	go h.Log("ZSet-ZRemRangeByScore", []interface{}{key, start, end}, count, err)
	return
}

// 返回有序集中指定索引区间内的成员，分数从高到低
func (h *Handler) ZRevRange(key string, start, end int64) (members []string, err error) {
	members, err = h.Client.ZRevRange(context.Background(), key, start, end).Result()
	go h.Log("ZSet-ZRevRange", []interface{}{key, start, end}, members, err)
	return
}

// 返回有序集中指定分数区间内的成员，分数从高到低排序
func (h *Handler) ZRevRangeByScore(key string, opt *redis.ZRangeBy) (members []string, err error) {
	members, err = h.Client.ZRevRangeByScore(context.Background(), key, opt).Result()
	go h.Log("ZSet-ZRevRangeByScore", []interface{}{key, opt}, members, err)
	return
}

// 返回有序集合中指定成员的排名，有序集成员按分数值递减(从大到小)排序
func (h *Handler) ZRevRank(key string, member string) (rank int64, err error) {
	rank, err = h.Client.ZRevRank(context.Background(), key, member).Result()
	go h.Log("ZSet-ZRevRank", []interface{}{key, member}, rank, err)
	return
}

// 返回有序集中，成员的分数值
func (h *Handler) ZScore(key string, member string) (score float64, err error) {
	score, err = h.Client.ZScore(context.Background(), key, member).Result()
	go h.Log("ZSet-ZScore", []interface{}{key, member}, score, err)
	return
}

// 计算给定的一个或多个有序集的并集，并存储在新的 key 中
func (h *Handler) ZUnionStore(destination string, opt *redis.ZStore) (count int64, err error) {
	count, err = h.Client.ZUnionStore(context.Background(), destination, opt).Result()
	go h.Log("ZSet-ZUnionStore", []interface{}{destination, opt}, count, err)
	return
}

//迭代有序集合中的元素（包括元素成员和元素分值）
func (h *Handler) ZScan(key string, cursor uint64, match string, count int64) (scoreMember []string, nextCursor uint64, err error) {
	scoreMember, nextCursor, err = h.Client.ZScan(context.Background(), key, cursor, match, count).Result()
	go h.Log("ZSet-ZScan", []interface{}{key, cursor, match, count}, []interface{}{scoreMember, nextCursor}, err)
	return
}
